/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQml 2.12
import QtGraphicalEffects 1.0
import org.ukui.tablet 1.0

Item {
    property int iconState: LauncherItem.Normal
    property bool select: true;

    property string icon;
    property int iconWidth: 100;
    property int iconRadius: 10 * Style.tabletScaleFactor;

    property int deleteiconWidth: 30;
    property int selecticonWidth: 30;

    signal unload(var appid);
    signal checked(var appid);
    signal unchecked(var appid);

    signal deleteIcon()

    state: {
        switch (iconState) {
        case LauncherItem.Normal: return "Normal";
        case LauncherItem.Editable: return "Editable";
        case LauncherItem.Selectable: return "Selectable";
        }
    }

    states: [
        State {
            name: "Normal"
            StateChangeScript {
                script: {
                    if (deleteiconAnimation.running)
                        deleteiconAnimation.stop();
                    if (appiconAnimation.running)
                        appiconAnimation.stop();
                    deleteicon.scale = 0;
                    selecticon.scale = 0;
                    appicon.rotation = 0;
                    deleteicon.rotation = 0;
                }
            }
        },
        State {
            name: "Editable"
            StateChangeScript {
                script: {
                    deleteiconAnimation.start();
                    appiconAnimation.start();
                }
            }
        },
        State {
            name: "Selectable"
            StateChangeScript {
                name: "myScript"
                script: { selecticonAnimation.start(); }
            }
        }
    ]

    width: iconWidth
    height: iconWidth

    Rectangle {
        id: mask;
        width: iconWidth;
        height: iconWidth;
        radius: iconRadius;
        visible: false;
    }
    Image {
        id: image;
        source: icon;
        anchors.centerIn: mask;
        width: iconWidth
        height: iconWidth
        sourceSize.width: iconWidth;
        sourceSize.height: iconWidth;
        visible: false;
        smooth: true

    }
    OpacityMask {
        id: appicon;
        anchors.fill: image;
        source: image;
        maskSource: mask;
    }

    Image {
        id: deleteicon;
        anchors {
            left: appicon.right; leftMargin: -deleteiconWidth/2;
            top: appicon.top; topMargin: -deleteiconWidth/5;
        }
        source: deleteMouseArea.pressed ? "qrc:/ui/images/icon_delete_click.svg" :
                                    "qrc:/ui/images/icon_delete_default.svg"
        MouseArea {
            id: deleteMouseArea
            anchors.fill: parent
            onClicked: deleteIcon()
        }
    }
    ScaleAnimator {
        id: deleteiconAnimation;
        target: deleteicon;
        from: 0;
        to: 1;
        duration: 500;
    }
    SequentialAnimation {
        id: appiconAnimation;
        loops: AnimatedSprite.Infinite;
        ParallelAnimation {
            RotationAnimation {
                target: appicon;
                from: 0;
                to: 2;
                duration: 150;
            }
            RotationAnimation {
                target: deleteicon;
                from: 0;
                to: 5;
                duration: 120;
            }
        }
        ParallelAnimation {
            RotationAnimation {
                target: appicon;
                from: 2;
                to: -2;
                duration: 150;
            }
            RotationAnimation {
                target: deleteicon;
                from: 2;
                to: -5;
                duration: 120;
            }
        }
    }


    Rectangle {
        id: selecticon;
        width: selecticonWidth;
        height: selecticonWidth;
        radius: selecticonWidth/2;
        color: "white";
        scale: 0;
        anchors.left: appicon.right;
        anchors.leftMargin: -selecticonWidth/2;
        anchors.top: appicon.top;
        anchors.topMargin: -selecticonWidth/5;
        Text {
            id: textcolor;
            text: qsTr("✓");
            color: "gray";
            anchors.centerIn: parent;
            font.bold: true;
        }
        MouseArea {
            anchors.fill: parent;
            onClicked: {
                if(select) {
                    textcolor.color = "gray";
                    selecticon.color = "white";
                    select = false;
                    unchecked(appicon);
                }
                else {
                    textcolor.color = "white";
                    selecticon.color = "gray";
                    select = true;
                    checked(appicon);
                }
            }
        }
    }
    ScaleAnimator {
        id: selecticonAnimation;
        target: selecticon;
        from: 0;
        to: 1;
        duration: 500;
    }

    Component.onCompleted: {
        if(select) {
            textcolor.color = "white";
            selecticon.color = "gray";
        }
        else {
            textcolor.color = "gray";
            selecticon.color = "white";
        }
        if(iconState === LauncherItem.Selectable) {
            selecticonAnimation.start();
        }
    }
}
